package cn.yan.luowowo.controller;

import cn.yan.luowowo.domain.Destination;
import cn.yan.luowowo.domain.Region;
import cn.yan.luowowo.query.RegionQuery;
import cn.yan.luowowo.service.DestinationService;
import cn.yan.luowowo.service.RegionService;
import cn.yan.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    @Autowired
    private DestinationService destinationService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")RegionQuery qo){

        Page<Region> page = regionService.query(qo);

        model.addAttribute("page",page);

        return "region/list";

    }


    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep) throws Exception{
        //查询
       return destinationService.list();

    }

    //修改or添加
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    //根据地区Id查询目的
    @RequestMapping("getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){

       return destinationService.queryByRegionId(rid);

    }

    //删除区域
    @RequestMapping("/delete")
    @ResponseBody
    public Object deleteRegion(String id){

        regionService.delete(id);

        return JsonResult.success();
    }


    //设置热门&取消热门
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(String id,int hot){
        regionService.changeHotValue(id,hot);

        return JsonResult.success();
    }

}
